package com.yb.framework.neo4j.utils;

import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.StatementResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Neo4jUtil {
	
	@Autowired
	private Driver neo4jDriver;
	
	private Logger logger = LoggerFactory.getLogger(Neo4jUtil.class);
	
	public boolean isNeo4jOpen() {
		try (Session session = neo4jDriver.session()) {
			logger.info("连接成功：" + session.isOpen());
			return session.isOpen();
		} catch (Exception e) {
			logger.error("连接失败：", e);
		}
		return false;
	}
	
	public StatementResult excuteCypherSql(String cypherSql) {
		StatementResult result = null;
		try (Session session = neo4jDriver.session()) {
			System.out.println(cypherSql);
			result = session.run(cypherSql);
			session.close();
		} catch (Exception e) {
			throw e;
		}
		return result;
	}
	
	// 去掉引号（用于属性添加）
	public String getPropertiesJson(String jsonStr) {
		String propertiesJsonStr = jsonStr.replaceAll("\"(\\w+)\"(\\s*:\\s*)", "$1$2"); // 去掉key的引号
		return propertiesJsonStr;
	}
}
